package com.yupi.springbootinit.mq;

import com.rabbitmq.client.*;
import org.checkerframework.checker.units.qual.A;

import java.util.HashMap;

public class ReceiveLogs {
    private static final String EXCHANGE_NAME = "logs";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-message-ttl",8000);
        Channel channel = connection.createChannel(); //队列1
        Channel channel2 = connection.createChannel();//队列2
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT); //这是交换机，通用的
        String queueName = "1的队列";
        String queueName2 = "2的队列";
        channel.queueDeclare(queueName,true,false,false,args);
        channel.queueDeclare(queueName2,true,false,false, args);
        channel.queueBind(queueName, EXCHANGE_NAME, "");
        channel2.queueBind(queueName2, EXCHANGE_NAME, "");



        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [1] Received '" + message + "'");
        };

        DeliverCallback deliverCallback2 = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [2] Received '" + message + "'");
        };

        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> { });
        channel2.basicConsume(queueName2, true, deliverCallback2, consumerTag -> { });
    }
}